﻿Imports System.IO.IsolatedStorage

Namespace Utility
    Public Module ConfigHelper
        Public Function HasConfig(name As String) As Boolean
            Return IsolatedStorageSettings.ApplicationSettings.Contains(name)
        End Function

        Public Sub Save(Of T)(name As String, value As T)
            Dim settings = IsolatedStorageSettings.ApplicationSettings
            If Not settings.Contains(name) Then
                settings.Add(name, value)
            Else
                settings(name) = value
            End If
            settings.Save()
        End Sub

        Public Function Load(Of T)(name As String) As T
            If IsolatedStorageSettings.ApplicationSettings.Contains(name) Then
                Return CType(IsolatedStorageSettings.ApplicationSettings(name), T)
            End If

            Return Nothing
        End Function

        Public Function Load(Of T)(name As String, defvalue As T) As T
            Dim s = Load(Of T)(name)
            If s Is Nothing Then
                Return defvalue
            Else
                Return s
            End If
        End Function

        Public Sub Remove(name As String)
            If IsolatedStorageSettings.ApplicationSettings.Contains(name) Then
                IsolatedStorageSettings.ApplicationSettings.Remove(name)
            End If
        End Sub
    End Module
End Namespace